package com.example.lopngoaingu;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tttrung43.libs.android.Connection;
import tttrung43.libs.android.DBAdapter;
import android.app.Activity;
import android.app.ProgressDialog;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;

public class KhmerActivity extends Activity{
	
	WebView web,webVoca, webConversation;	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.khmer_layout);		
		//new LoadDanhNgon().execute();
		web = (WebView)findViewById(R.id.web);
		webVoca = (WebView)findViewById(R.id.webVoca);
		webConversation = (WebView)findViewById(R.id.webConversation);
			
		//web.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		//webVoca.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		//webConversation.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		
		String data="<html>"
				+"<body>"
				+"<img src='http://2.lopngoaingu.com/listening/streamline-english/Directions/img/49a.jpeg'/>"
				+"</body>"
				+"</html>";
		web.loadDataWithBaseURL(null, data, "text/html", "utf-8", null);
		
		String data1="<html>"
				+"<body>"
				+"<img src='http://2.lopngoaingu.com/listening/streamline-english/Directions/img/49b.jpeg'/>"
				+"</body>"
				+"</html>";
		webVoca.loadDataWithBaseURL(null, data1, "text/html", "utf-8", null);
		
		String data2="<html>"
				+"<body>"
				+"<img src='http://2.lopngoaingu.com/listening/streamline-english/Directions/img/49c.jpeg'/>"
				+"</body>"
				+"</html>";
		webConversation.loadDataWithBaseURL(null, data2, "text/html", "utf-8", null);
		
	}
	
	public void btnGrammar_Click(View v){
		web.setVisibility(View.VISIBLE);
		webVoca.setVisibility(View.GONE);
		webConversation.setVisibility(View.GONE);
	}

	public void btnVoca_Click(View v){
		web.setVisibility(View.GONE);
		webVoca.setVisibility(View.VISIBLE);
		webConversation.setVisibility(View.GONE);
	}
	
	public void btnConversation_Click(View v){
		web.setVisibility(View.GONE);
		webVoca.setVisibility(View.GONE);
		webConversation.setVisibility(View.VISIBLE);
	}
	
	void CreateSubject(){
		Connection conn = new Connection(this);
		conn.AddData("insert into subjectquotation(name) values('Cuộc sống')");
		conn.AddData("insert into subjectquotation(name) values('Tình yêu')");
		conn.AddData("insert into subjectquotation(name) values('Tình bạn')");
		conn.AddData("insert into subjectquotation(name) values('Sự tử tế')");
		conn.AddData("insert into subjectquotation(name) values('Sự lo lắng')");
		conn.AddData("insert into subjectquotation(name) values('Hài hước')");
		conn.AddData("insert into subjectquotation(name) values('Thành công')");
		conn.AddData("insert into subjectquotation(name) values('Công việc')");
		conn.AddData("insert into subjectquotation(name) values('Thời gian')");
		conn.AddData("insert into subjectquotation(name) values('Sự thật')");
		conn.AddData("insert into subjectquotation(name) values('Khoan dung')");
		conn.AddData("insert into subjectquotation(name) values('Thuế')");
		conn.AddData("insert into subjectquotation(name) values('Ngân hàng')");
		conn.AddData("insert into subjectquotation(name) values('Luật sư')");
		conn.AddData("insert into subjectquotation(name) values('Hạnh phúc')");
		conn.AddData("insert into subjectquotation(name) values('Ước mơ')");
		conn.AddData("insert into subjectquotation(name) values('Gia đình')");
		conn.AddData("insert into subjectquotation(name) values('Quyến rũ')");
		conn.AddData("insert into subjectquotation(name) values('Khả năng')");
		conn.AddData("insert into subjectquotation(name) values('Sự thay đổi')");
		conn.AddData("insert into subjectquotation(name) values('Giáo dục')");
		conn.AddData("insert into subjectquotation(name) values('Tiền bạc')");
		conn.AddData("insert into subjectquotation(name) values('Thông minh')");
		conn.AddData("insert into subjectquotation(name) values('Cảm xúc')");
		conn.AddData("insert into subjectquotation(name) values('Sức khỏe')");
		conn.Close();
	}
	
	
	
	class LoadDanhNgon extends AsyncTask<Void, Void, Void>{

		DBAdapter conn;
		SQLiteDatabase db;
		ProgressDialog pDialog;
		public LoadDanhNgon(){		
			//Create();
			pDialog = new ProgressDialog(KhmerActivity.this);
			pDialog.setMessage("Loading");
			pDialog.show();
		}
		
		@Override
		protected Void doInBackground(Void... params) {	
			//LoadSubject();
			conn = new DBAdapter(KhmerActivity.this);
			db = conn.getWritableDatabase();
			//LoadSubject("http://2.lopngoaingu.com/listening/streamline-english/Departure/index.php?id=1", "Departure", 1);
			//LoadSubject("http://2.lopngoaingu.com/listening/streamline-english/Connections/index.php?id=1", "Connections", 2);
			//LoadSubject("http://2.lopngoaingu.com/listening/streamline-english/Destinations/index.php?id=1", "Destinations", 3);
			LoadSubject("http://2.lopngoaingu.com/listening/streamline-english/Directions/index.php?id=1", "Directions", 4);
			db.close();
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			pDialog.dismiss();
			super.onPostExecute(result);
		}
		
		
		void Create(){
			Connection conn = new Connection(KhmerActivity.this);
			conn.AddData("insert into subject(name) values('Departure')");
			conn.AddData("insert into subject(name) values('Connections')");
			conn.AddData("insert into subject(name) values('Destinations')");
			conn.AddData("insert into subject(name) values('Directions')");
		}
		
		void LoadSubject(String urlSubject, String SubjectName, int subID){			
			try {
				Document doc = Jsoup.connect(urlSubject).get();
				Element form = doc.select("select").first();
				Elements options = form.select("option");
				
				
				for(int i=1;i<options.size();i++){
					String text = options.get(i).text();					
					String title = text.substring(text.indexOf(" ")+1);
					String strGrammar = "http://2.lopngoaingu.com/listening/streamline-english/"+SubjectName+"/img/"+i+"a.jpeg";
					String strVoca = "http://2.lopngoaingu.com/listening/streamline-english/"+SubjectName+"/img/"+i+"b.jpeg";
					String strConversation ="http://2.lopngoaingu.com/listening/streamline-english/"+SubjectName+"/img/"+i+"c.jpeg";
					String strSound ="http://2.lopngoaingu.com/listening/streamline-english/"+SubjectName+"/audio/"+i+".mp3";
					URL url =new URL(strSound);
					HttpURLConnection network = (HttpURLConnection)url.openConnection();
					if(network.getResponseCode()==404){
						strSound="404";
						Log.e("Not found","File Not found");
					}
					String[] params = new String[]{title,strGrammar,strVoca,strConversation,strSound,""+subID,""+i,"0"};
					db.execSQL("insert into unit(title,grammar,vocabulary,conversation,sound,subjectid,unitorder,offline) values(?,?,?,?,?,?,?,?)", params);
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		void DanhNgonCuocSong(String url,int subject){
			try {				
				Document doc = Jsoup.connect(url).get();
				Elements elements = doc.select(".dag_right .dag_te");
				Elements authors = doc.select(".dag_right .dag_te_name .dag_tg");
				SQLiteDatabase db = conn.getWritableDatabase();
				for(int i=0;i<elements.size();i++){
					String title = elements.get(i).text();
					String meaning = elements.get(i).select("span").attr("onmouseover").replace("showmessage('", "").replace("',this)", ""); 	
					String author = authors.get(i).text();											
					db.execSQL("insert into quotations(title, meaning, author, subjectid) values(?,?,?,?)", new String[]{title, meaning, author,""+subject});					
				}
				db.close();
				Log.i("Element Count",""+elements.size());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
